package cn.bjit.controller;


import cn.bjit.enums.BizCodeEnum;
import cn.bjit.exception.BizException;
import cn.bjit.model.AddressDO;
import cn.bjit.request.AddressAddReqeust;
import cn.bjit.service.AddressService;
import cn.bjit.util.JsonData;
import cn.bjit.vo.AddressVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 电商-公司收发货地址表 前端控制器
 * </p>
 *
 * @author yx
 * @since 2022-05-29
 */
@Api(tags = "收货地址模块")
@RestController
@RequestMapping("/api/address/v1")
public class AddressController {
    @Resource
    private AddressService addressService;

    @ApiOperation("新增收货地址")
    @PostMapping("add")
    public JsonData add(@ApiParam("地址对象") @RequestBody AddressAddReqeust addressAddReqeust) {
        addressService.add(addressAddReqeust);
        return JsonData.buildSuccess();
    }

    @ApiOperation("根据id查找地址详情")
    @GetMapping("/find/{address_id}")
    public JsonData findById(@ApiParam(value = "地址id", required = true) @PathVariable("address_id") long addressId) {

        AddressVO addressVO = addressService.findById(addressId);

        return addressVO == null ? JsonData.buildResult(BizCodeEnum.ADDRESS_NO_EXITS) : JsonData.buildSuccess(addressVO);
    }

    @ApiOperation("删除指定的收货地址")
    @GetMapping("/del/{address_id}")
    public JsonData del(@ApiParam(value = "要删除的收货地址的id") @PathVariable("address_id") long addressId) {
        int row = addressService.del(addressId);
        return row == 1 ? JsonData.buildSuccess() : JsonData.buildResult(BizCodeEnum.ADDRESS_DEL_FAIL);
    }
    @ApiOperation("查询所有的收货地址")
    @GetMapping("/list")
    public JsonData findUserAllAddress(){
        List<AddressVO> addressVOList= addressService.findUserAllAddress();
        return JsonData.buildSuccess(addressVOList);
    }
}

